<script setup>
import Navbar from './components/Navbar.vue'
import Carousel from '@/components/Carousel/Carousel.vue';
import CateScroll from '@/components/CateScroll/CateScroll.vue';
import Guess from '@/components/Guess/Guess.vue';

import { getBannersAPI, getCategoryAPI, getHotsAPI, getLikesAPI, getNewGoodsAPI } from '@/api/home'
import { ref } from 'vue';

// 获取轮播图
const banners = ref([])
const getBanners = async () => {
  const res = await getBannersAPI()
  banners.value = res.result
}
getBanners()

// 获取分类
const category = ref([])
const getCategory = async () => {
  const res = await getCategoryAPI()
  category.value = res.result
  console.log(category.value);
}
getCategory()

// 热门推荐
const hots = ref([])
const getHots = async () => {
  const res = await getHotsAPI()
  hots.value = res.result
}
getHots()

// 新鲜好物
const newGoods = ref([])
const getNewGoods = async () => {
  const res = await getNewGoodsAPI()
  // 因为所有的接口都跟后端约定数据放在 result 属性中
  newGoods.value = res.result
  console.log('新鲜好物', newGoods.value);
}
getNewGoods()

// 猜你喜欢
const likes = ref([])
const page = ref(1)
const totalPage = ref(0)
const isLoading = ref(false)

const getLikes = async () => {
  // 开始加载将加载中状态改为 true
  isLoading.value = true

  const res = await getLikesAPI(page.value)

  // 拿到后台数据将总页数记起来
  totalPage.value = res.pages

  likes.value = [
    ...likes.value,
    ...res.result.items
  ]

  // 数据已经处理完毕 将加载中状态改回 false
  isLoading.value = false


}
// 页面进来, 加载第一页猜你喜欢
getLikes()

// 加载更多
const loadMore = async () => {
  // 特殊情况一, 上一次加载还没完成, 不能进行下一次
  if (isLoading.value) return uni.showToast({ title: '请耐心等待', icon: 'none' })
  // 特殊情况二, 如果已经到底, 不能再拿
  if (page.value === totalPage.value) return uni.showToast({ title: '已经是最后一页', icon: 'none' })

  // 每次拉倒第都要先将页码+1再次触发猜你喜欢
  page.value++
  getLikes()
}

// 下拉刷新
const isRefreshing = ref(false)
const refresh = async () => {
  isRefreshing.value = true
  // console.log('准备刷新');
  // 基本逻辑就是全部数据加载函数, 调用一遍
  // 如果有一些会被永久改变的数据, 要手动复原, 比如当前正在翻页的页码
  // 如果偷懒, 全部复原也行
  banners.value = []
  category.value = []
  hots.value = []
  newGoods.value = []
  likes.value = []
  page.value = 1
  totalPage.value = 0
  isLoading.value = false

  // Promise.all 是一个内置函数
  // 通过传入promise异步操作组成的数组, 会返回一个新的异步对象
  // 在所有传入的异步操作成功后, 这个新返回的异步对象才算成功
  // 其他的 Promise 函数还有 race 和 allSettle
  // race 是传入的数组, 任意一个完成, 即可
  // allSettle 是传入的数组全部敲定的时候
  
  await Promise.all([
    getBanners(),
    getCategory(),
    getHots(),
    getNewGoods(),
    getLikes(),
  ])

  isRefreshing.value = false
}


</script>

<template>
  <!-- 顶部导航 -->
  <Navbar />

  <scroll-view scroll-y @scrolltolower="loadMore" :refresher-enabled="true" @refresherrefresh="refresh" :refresher-triggered="isRefreshing">
    <!-- 轮播图 -->
    <Carousel :banners="banners" />
    <!-- 分类 -->
    <CateScroll :category="category" />

    <!--  热门推荐 -->
    <view class="panel recommend">
      <view class="item" v-for="item in hots" :key="item.id">
        <view class="title">
          {{ item.title }}<text>{{ item.alt }}</text>
        </view>
        <navigator hover-class="none" :url="`/pages/recommend/index?type=${item.type}`" class="cards">
          <image mode="aspectFit" v-for="img in item.pictures" :key="img" :src="img"></image>
        </navigator>
      </view>
    </view>

    <!-- 新鲜好物 -->
    <view class="panel fresh">
      <view class="title">
        新鲜好物
        <navigator hover-class="none" class="more" url="/pages/recommend/index?type=5">更多</navigator>
      </view>
      <view class="cards">
        <navigator hover-class="none" :url="`/pages/goods/index?id=${item.id}`" v-for="item in newGoods" :key="item.id">
          <image mode="aspectFit" :src="item.picture"></image>
          <view class="name">{{ item.name }}</view>
          <view class="price">
            <text class="small">¥</text>{{ item.price }}
          </view>
        </navigator>
      </view>
    </view>

    <!-- 热门品牌 -->
    <view class="panel brands">
      <view class="title">
        热门品牌
        <navigator hover-class="none" class="more" url="/pages/list/index">更多</navigator>
      </view>
      <view class="cards">
        <navigator hover-class="none" url="/pages/goods/index">
          <image mode="aspectFit" src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"></image>
          <view class="name">小米</view>
          <view class="price">99元起</view>
        </navigator>
        <navigator hover-class="none" url="/pages/goods/index">
          <image mode="aspectFit" src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"></image>
          <view class="name">TCL</view>
          <view class="price">199起</view>
        </navigator>
        <navigator hover-class="none" url="/pages/goods/index">
          <image mode="aspectFit" src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"></image>
          <view class="name">饭小宝</view>
          <view class="price">9.9起</view>
        </navigator>
        <navigator hover-class="none" url="/pages/goods/index">
          <image mode="aspectFit" src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"></image>
          <view class="name">鳄鱼</view>
          <view class="price">299起</view>
        </navigator>
      </view>
    </view>

    <Guess :list="likes" />

  </scroll-view>
</template>
  
<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}

/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;

  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;

    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }

  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }

  .cards {
    display: flex;
  }

  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;

  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;

    &:nth-child(2n) {
      border-right: 0 none;
    }

    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }

  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;

    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;

  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }

  .small {
    font-size: 80%;
  }
}

.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}

.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}

.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}

.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>